import module namespace page="http://sendmeafilter.com/common/page" at "../common/page.xqy";
import module namespace order="http://sendmeafilter.com/data/order" at "../data/order.xqy";

declare boundary-space preserve;
declare option xdmp:mapping "false";

declare variable $checks := ('Select');
declare variable $space :="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

page:page (
	(
		page:meta('title', 'Orders - SendMeAFilter.com'),
		page:meta('backoffice')
	),

	<div class="wrapper">
		<div class="caption">	
			<div float="right">
				<h3>Orders</h3>
			<div id="actions">
				<table class="list-table">
					<tr>
						<td>
							Search:&nbsp;
                           <input type="text" style="width: 150px;" name="keywords" value="" onchane="return Submit()"/>&nbsp;
							<select class="facets" name="view" id="select" size="1" onChange="return Submit()">
								<option value="needship" selected="selected">
									<span>need to ship</span>
								</option>
								<option value="view">
									<span class="all">waiting for payment</span>
								</option>
								<option value="view">
									<span class="all">waiting for time</span>
								</option>
								<option value="view">
									<span class="all">backordered</span>
								</option>
								<option value="view">
									<span class="all">fulfilled</span>
								</option>
								<option value="view">
									<span class="all">all</span>
								</option>
							</select>&nbsp;
							<select class="facets" name="order" id="select" size="1" onChange="return Submit()">
								<option value="payfirst" selected="selected">
									<span class="all">today</span>
								</option>
								<option value="payfirst">
									<span class="all">this week</span>
								</option>
								<option value="payfirst">
									<span class="all">this month</span>
								</option>
								<option value="activity">
									<span>ytd</span>
								</option>
								<option value="activity">
									<span>all</span>
								</option>
							</select>&nbsp;
							<input id="refreshButton" type="submit" value="Refresh"/>
						</td>
						<td align="right">
							<select class="facets" name="Action" id="select" size="1" onChange="return Submit()">
								<option value="labels" selected="selected">
									<span class="all">action</span>
								</option>
								<option value="labels">
									<span class="all">select all</span>
								</option>
								<option value="labels">
									<span class="all">deselect all</span>
								</option>
								<option value="labels">
									<span class="all">export labels</span>
								</option>
								<option value="ship">
									<span class="all">mark as shipped</span>
								</option>
								<option value="backorder">
									<span class="all">mark as backordered</span>
								</option>
								<option value="cancel">
									<span class="all">cancel order</span>
								</option>
							</select>&nbsp;
							<input id="actButton" type="submit" value="Act"/>
						</td>
					</tr>
				</table>
			</div>
			</div>
				
				<div id="entry">
					<table class="list-table">
					{
					let $orderedOrders:= for $o in $order:exampleOrders/order order by xs:date($o/nextShipping) return $o
					for $order at $i in $orderedOrders
					 return (
					if($i mod 6 = 1) then
					(
						if($i = 1) then () else <tr class="bg1"><td colspan="100">&nbsp;</td></tr>,
						<tr class="tableheader">
							<td>Id</td>
							<td>Order</td>
							{ for $check in $checks return <td><center>{$check}</center></td> }
						</tr>
					)
					else (),
					
					<tr class="{if($i mod 2 = 1) then 'bg1' else 'bg1-mid' }">
					<td><span class="oid">{fn:data($order/@orderid)}</span><br/>{$order/nextShipping/text()}</td>
					<td>{
							fn:string-join((
								$order/address/line,
								$order/address/city,',',
								$order/address/state,
								$order/address/zip)," ")
						}
						<br/>
						<ol>
							{	
							for $item at $x in $order/items/item 
							return(
								element li{
									fn:string-join((
										$item/quantity,
										"-",
										$item/productName,
										$item/size/height,
										"X",
										$item/size/width,
										"X",
										$item/size/thickness)," ")
										}
									)
								}
						</ol>
						
						
					</td>
					{
					for $check in $checks
					let $checkid := fn:lower-case($check)
					return
					<td>
					<center>
					{
					$space,
					element input {
					attribute type {"checkbox"},
					attribute name { fn:concat($order/@orderid,'-',$checkid)},
					attribute value {$checkid},
					if($order/orderStates/orderState[@id=fn:lower-case($check)]) then
					attribute checked { 'checked' }
					else ()
					},
					$space
					}
					</center>
					</td>
					}
					</tr>
					)}
				</table>
			</div>
		</div>
	</div>

)